#****************************************************************************
# Copyright (C) 2001-2004  PEAK System-Technik GmbH
#
# linux@peak-system.com
# www.peak-system.com
#
# Maintainer(s): Klaus Hitschler (klaus.hitschler@gmx.de)
# Contributions: Grant Edwards (gedwards@rivatek.com)
#****************************************************************************

#****************************************************************************
#
# Makefile for Peak CAN interface drivers (ISA, PCI, DONGLE, USB)
# with obfuscation for USB kernel only
#
# $Log: Makefile,v $
# Revision 1.38  2004/08/28 20:40:00  klaus
# Other handling of kbuild parameters for kernels less than 2.6.5
#
# Revision 1.37  2004/08/15 09:44:52  klaus
# rpm building process improved
#
# Revision 1.36  2004/07/18 14:14:47  klaus
# use the M=... instead of SUBDIRS=... method for kernel 2.6 make
#
# Revision 1.35  2004/05/13 15:32:58  klaus
# devfs is depreciated, moved handling from Makefile to pcan_common.h
#
# Revision 1.34  2004/05/08 10:18:21  klaus
# Added test for file version.h prior to resolve version from this file
#
# Revision 1.33  2004/05/06 18:47:15  klaus
# Improvements triggered by Mandrake distribution 2.6.3-7mdk: VERSION script
#
# Revision 1.32  2004/05/02 15:56:51  klaus
# Last corrections and improvements, especially for Makefile(s) and build scenarios
#
#****************************************************************************

#****************************************************************************
# targets:
# depend	    creates dependencies from included header files - not kernel 2.6
# all		      does a standard make of the driver
# clean		    cleans up, base for a forced new make
# install	    run as root, installs the drivers
# rpminstall	only for rpm installation, do not run interactively
# obfuscate	  only for Peak System-Technik use
# message     show info about build environment
#
# make options:
# DBG=DEBUG                 or NO_DEBUG    		  switch debug on and switches Makefile verbosity on
# KERNEL_LOCATION=...       where your kernel sources are located if not standard location (cross-compile)
# MOD=NO_MODVERSIONS		    or MODVERSIONS 		  compile for modversions use
# PAR=NO_PARPORT_SUBSYSTEM	or PARPORT_SUBSYSTEM 	compile for use without parport subsystem und pcan-dongle
# USB=NO_USB_SUPPORT 		    or USB_SUPPORT    	compile for use with USB and pcan-usb
# DEVFS=NO_DEVFS_SUPPORT    or DEVFS_SUPPORT    compile to support device file system - not kernel 2.6
# VERBOSE=0                 or 1                switch Makefile verbosity on
# ORG=NO                    or YES              take always NO, only for Peak System-Technik use
#
# please the first time do a make clean; make
#

#****************************************************************************
# please modify only here if your kernel path is elsewhere located
#
KERNEL_LOCATION = /usr/src/linux

#****************************************************************************
# removed old kernel source path retrival for support of kernels < 2.2.18!
# old KERNSRC = /lib/modules/$(VERSION)/build
#
KERNSRC := $(KERNEL_LOCATION)

#****************************************************************************
# whole makefile is based on availability of version.h file
# do a forced stop if it is not available
#
HASVERSION_H := $(shell if test -f $(KERNEL_LOCATION)/include/linux/version.h ; then echo yes ; else echo no; fi )

ifeq ($(HASVERSION_H),no)
$(error "Can't find $(KERNEL_LOCATION)/include/linux/version.h !")
else

#****************************************************************************
# removed old version retrival for better cross compile support
# old VERSION := $(shell uname -r)
#
VERSION := $(shell cpp -dM -I$(KERNEL_LOCATION)/include $(KERNEL_LOCATION)/include/linux/version.h \
            | grep UTS_RELEASE | sed -e 's;[^"]*"\(.*\)";\1;g')

ifeq ($(VERSION),)
VERSION := $(shell cpp -dM -I$(KERNEL_LOCATION)/include $(KERNEL_LOCATION)/include/linux/utsrelease.h \
            | grep UTS_RELEASE | sed -e 's;[^"]*"\(.*\)";\1;g')
endif

#****************************************************************************
# where the driver should be installed - change here for cross install
#
INSTALL_LOCATION = /lib/modules/$(VERSION)/misc

#****************************************************************************
# get extracted kernel VERSION and PATCHLEVEL for comparison
# decide to use KBUILD for kernels greater 2.6.0
#
KVERSION    := $(shell echo $(VERSION) | sed -e 's;\([1-9]\)\..*;\1;g' )
KPATCHLEVEL := $(shell echo $(VERSION) | sed -e 's;[1-9]\.\([0-9]\{0,3\}\)\..*;\1;g' )
SUBLEVEL    := $(shell echo $(VERSION) | sed -e 's;[1-9]\.[0-9]\{0,3\}\.\([0-9]\{0,3\}\).*;\1;g' )
USEKBUILD   := $(shell if [ $(KVERSION) -gt 2 ] || [ $(KVERSION) -eq 2 ] && [ $(KPATCHLEVEL) -ge 6 ]  ; \
                 then echo "yes" ; else echo "no"  ; fi)

#****************************************************************************
# some common switches and defines
#
DBG     = NO_DEBUG
MOD     = NO_MODVERSIONS
PAR     = NO_PARPORT_SUBSYSTEM
USB     = USB_SUPPORT
DEVFS   = NO_DEVFS_SUPPORT

VERBOSE = 1

ORG     = NO

OBF     = obfuscate
SRC     = src
TEMP    = /tmp

PARMS   = type=isa,sp        # only for installer: please modify the install parameters according to your configuration


#****************************************************************************
# switch make verbosity on
#
ifeq ($(DBG),DEBUG)
VERBOSE = 1
endif

#****************************************************************************
# preparation what to build or what to KBUILD
#
ifeq ($(USB),USB_SUPPORT)
ifeq ($(ORG),YES)
pcan-y :=  $(SRC)/pcan_main.o $(SRC)/pcan_fops.o $(SRC)/pcan_pci.o $(SRC)/pcan_isa.o $(SRC)/pcan_dongle.o $(SRC)/pcan_fifo.o \
$(SRC)/pcan_sja1000.o $(SRC)/pcan_parse.o $(SRC)/pcan_usb.o $(SRC)/pcan_usb_kernel_org.o
else
pcan-y := $(SRC)/pcan_main.o $(SRC)/pcan_fops.o $(SRC)/pcan_pci.o $(SRC)/pcan_isa.o $(SRC)/pcan_dongle.o $(SRC)/pcan_fifo.o \
$(SRC)/pcan_sja1000.o $(SRC)/pcan_parse.o $(SRC)/pcan_usb.o $(SRC)/pcan_usb_kernel.o
endif
else
pcan-y :=  $(SRC)/pcan_main.o $(SRC)/pcan_fops.o $(SRC)/pcan_pci.o $(SRC)/pcan_isa.o $(SRC)/pcan_dongle.o $(SRC)/pcan_fifo.o \
$(SRC)/pcan_sja1000.o $(SRC)/pcan_parse.o
endif

#****************************************************************************
# for local installation only: sometimes /etc/modprobe.conf.local is used
#
MODCONF = /etc/modprobe.conf
MODINST = "install pcan /sbin/modprobe --ignore-install pcan; /usr/local/bin/pcan_make_devices 1"

#****************************************************************************
# what's the target
#
TARGET = pcan.ko
obj-m := pcan.o

#****************************************************************************
# add flags to standard flags
#
ccflags-y := -I$(PWD) -D$(DBG) -D$(MOD) -D$(PAR) -D$(USB) -D$(DEVFS)

#****************************************************************************
# the actual make line
#
CMDLINE = $(MAKE) -C $(KERNSRC) M=$(PWD) V=$(VERBOSE) $(filter-out all, $(MAKECMDGOLAS)) modules

#****************************************************************************
# do it
#
all : 	message
	@ echo "Stuff = $(PWD)"
	$(CMDLINE)

#********** clean all for a rebuild *****************************************
clean:
	rm -f $(SRC)/*o $(SRC)/*~ $(SRC)/.*cmd *o *~ .*cmd  pcan.mod.c

#********** this entry is reserved for root access only *********************
install:
	@if test -d $(INSTALL_LOCATION) ; then echo "info: $(INSTALL_LOCATION) exists."; else mkdir $(INSTALL_LOCATION); fi
	cp $(TARGET) $(INSTALL_LOCATION)/$(TARGET)
	cp pcan_make_devices /usr/local/bin/pcan_make_devices
	if test -z $(shell grep -l pcan $(MODCONF)); then\
		cp $(MODCONF) $(MODCONF)~;\
		echo ""                                               >> $(MODCONF);\
		echo "# pcan - automatic made entry, begin --------"  >> $(MODCONF);\
		echo "# options pcan $(PARMS)   # see documentation"  >> $(MODCONF);\
		echo $(MODINST)                                       >> $(MODCONF);\
		echo "# pcan - automatic made entry, end ----------"  >> $(MODCONF);\
		echo ""                                               >> $(MODCONF);\
	fi
	/sbin/depmod
	cp pcan.h /usr/include/pcan.h
	chmod 644 /usr/include/pcan.h

#********** informations during build of driver *****************************
.PHONY : message
message:
	@ echo "***"
	@ echo "*** Host machine kernel version=$(shell uname -r)"
	@ echo "*** Driver kernel version=$(VERSION)"
	@ echo "*** Path to kernel sources=$(KERNSRC)"
	@ echo "*** use KBUILD=$(USEKBUILD)"
	@ echo "***"

#********** make obfuscated translation *************************************
.PHONY : obfuscate
obfuscate:
	sed -e /COBF/,\$$d  $(SRC)/cobf.h             > $(TEMP)/cobf.h.header;\
	sed -e  /COBF/,\$$d  $(SRC)/pcan_usb_kernel.c > $(TEMP)/pcan_usb_kernel.c.header;\
	cobf -p $(OBF)/pp_gnu -o $(TEMP) -c pcan_usb_kernel.c -t $(OBF)/tlist.tok -m $(OBF)/mlist.tok -x pcan_ $(SRC)/pcan_usb_kernel_org.c;\
	cat $(TEMP)/pcan_usb_kernel.c.header  $(TEMP)/pcan_usb_kernel.c  > $(SRC)/pcan_usb_kernel.c;\
	cat $(TEMP)/cobf.h.header $(TEMP)/cobf.h > $(SRC)/cobf.h

endif  # <<<<< HASVERSION_H >>>>>>

# DO NOT DELETE

